const fs = require("fs");
const path = require("path");
const http = require("http");
let extObj = {
    ".html": "text/html;charset=utf8",
    ".css": "text/css;charset=utf8",
    ".jpg": "image/jpg",
    ".js": "application/javascript",
    ".ico": "image/x-icon"
}
const server = http.createServer((req, res) => {
    let {
        url
    } = req;
    let filePath = path.join(__dirname, "public", url);
    let ext = path.extname(url);
    // console.log(filePath);
    try {
        let file = fs.readFileSync(filePath);
        extObj[ext] && res.setHeader("content-type", extObj[ext]);
        res.end(file);
    } catch (err) {
        console.log(err);
        req.statusCode = 404;
        res.end("404");
    }

})

server.listen(8001, () => {
    console.log("ok");
})